﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RichTextEditor
{
    public partial class frmSave : Form
    {
        private frmMain parent;
        public frmMain MainForm
        {
            get { return this.parent; }
            set { this.parent = value; }
        }

        public frmSave()
        {
            InitializeComponent();
        }

        private bool shouldButtonOkBeEnabled()
        {
            if (!check_preciser.Checked)
                return true;
            if (cbox_semestre.SelectedIndex >= 0)
            {
                if (cbox_ue.SelectedIndex >= 0)
                {
                    if (cbox_year.SelectedIndex >= 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void check_preciser_CheckedChanged(object sender, EventArgs e)
        {
            if (check_preciser.Checked)
            {
                cbox_semestre.Enabled = true;
                cbox_year.Enabled = true;
                cbox_ue.Enabled = true;
            }
            else
            {
                cbox_semestre.Enabled = false;
                cbox_year.Enabled = false;
                cbox_ue.Enabled = false;
            }
            btn_ok_save.Enabled = shouldButtonOkBeEnabled();
        }

        private void btn_cancel_save_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbox_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_ok_save.Enabled = shouldButtonOkBeEnabled();
        }

        private void cbox_semestre_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_ok_save.Enabled = shouldButtonOkBeEnabled();
        }

        private void cbox_ue_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_ok_save.Enabled = shouldButtonOkBeEnabled();
        }

        int count = 0;
        private void btn_ok_save_Click(object sender, EventArgs e)
        {
            string path = frmMain.DOCS_PATH;
            switch (cbox_year.SelectedItem.ToString())
            {
                case "L1":
                    path += frmMain.L1_PATH;
                    break;
                case "L2":
                    path += frmMain.L2_PATH;
                    break;
            }

            string semestre = cbox_semestre.SelectedItem.ToString();
            if (!Directory.Exists(path + "\\" + semestre))
            {
                path += "\\" + semestre;
                Directory.CreateDirectory(path);
            }
            else
                path += "\\" + semestre;

            string ue = cbox_ue.SelectedItem.ToString();
            if (!Directory.Exists(path + "\\" + ue))
            {
                path += "\\" + ue;
                Directory.CreateDirectory(path);
            }
            else
                path += "\\" + ue;


            if (!String.IsNullOrEmpty(txt_nom_save.Text))
                path += "\\" + txt_nom_save.Text + ".rtf";
            else
            {
                path += "\\note" + count + ".rtf";
                count++;
            }

            parent.rtbDoc.SaveFile(path, RichTextBoxStreamType.RichText);
            parent.treeView_Load(frmMain.DOCS_PATH);
            parent.rtbDoc.Modified = false;
            this.Close();
        }

        private void frmSave_Load(object sender, EventArgs e)
        {
            txt_nom_save.Text = parent.Text.Substring(11);
        }
    }
}
